//
//  JCWFinancialFroductListVC.swift
//  HengYouCai
//
//  Created by wll on 2017/11/9.
//  Copyright © 2017年 lwj. All rights reserved.
//  理财产品列表--代码

import UIKit

///定义blcok
typealias RunloopBlock = ((IndexPath) -> Bool)
private let cellID = "JCWFinancialFroductListVCCell"

//MARK:界面初始化
class JCWFinancialFroductListVC: BaseTableViewController {

    //对应的列表类型
    var type:String?
    /******处理滑动卡顿******///iOS11 系统bug会特别卡
    ///时钟事件
    var timer: Timer?
    ///任务数组
    var tasksArr: [RunloopBlock] = []
    ///任务对应indexPath数组
    var tasksIndexPathArr: [IndexPath] = []
    ///最大任务 10
    let maxQueueLength = JCWLimitNumberPageSize
    /******处理滑动卡顿******/
    

    ///上拉下拉刷新管理器
    let  pageManager = JCWRefreshPageManager()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //每次进来刷新
        if pageManager.pageNo>1 {
            
            let index = IndexPath(row: 0, section: 0)
            self.tableView.scrollToRow(at: index, at: .top, animated: true)
        }
        pageManager.pageNo = 1
        
        loadData()

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.jcw_showGifLoading()
        ///界面配置
        self.createNav()
        self.createCV()
        /******处理滑动卡顿******/
        
        //添加runloop的监听
        self.addRunloopObserver()

        /******处理滑动卡顿******/
        
        weak var weakSelf = self
        let header = LWRefreshGifHeader(refreshingBlock: {
            weakSelf?.pageManager.pageNo = 1
            weakSelf?.loadData()
        })
        header?.isAutomaticallyChangeAlpha = true
        self.tableView.mj_header = header
    
    }
    
    func createNav() {
//        self.navigationItem.title = "理财产品"
    }

    func createCV() {
        
        self.tableView.register(JCWFinancialProductCell.self, forCellReuseIdentifier: cellID)
        self.tableView.backgroundColor = Normal_458Color
        self.tableView.separatorStyle = .none
        self.tableView.estimatedRowHeight = 0
        self.tableView.estimatedSectionFooterHeight = 0
        self.tableView.estimatedSectionHeaderHeight = 0
        ///顶部间隙
        let topGrayView = UIView(frame: CGRect(x: 0, y: 0, width: JCWSCREENW, height: 8))
        topGrayView.backgroundColor = self.tableView.backgroundColor
        self.tableView.tableHeaderView = topGrayView
    }
}





//MARK:表代理
extension JCWFinancialFroductListVC {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.pageManager.dataArr.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: JCWFinancialProductCell = tableView.dequeueReusableCell(withIdentifier: cellID) as! JCWFinancialProductCell
        self.addTask(indexPath) { (indexP) -> Bool in
            cell.kaDunHandler(self.pageManager.dataArr, tableView, indexP)
            return true
        }
        cell.kaDunHandler2(self.pageManager.dataArr, tableView, indexPath)
        return cell
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 128
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let final = FinancialDetailVC()
        let model = pageManager.dataArr[indexPath.row] as! ProjectModel
        
        final.homeModel = model
        final.idStr = model.id
        self.navigationController?.pushViewController(final, animated: true, withHideBottomBarWhenPush: true, pop: false)
        
    }
    
}

//MARK:处理卡顿
extension JCWFinancialFroductListVC {
    
    ///添加新的任务的方法!
    func addTask(_ indexP: IndexPath, unit: @escaping RunloopBlock) {
        self.tasksArr.append(unit)
        self.tasksIndexPathArr.append(indexP)
        //判断一下 保证没有来得及显示的cell不会绘制
        if self.tasksArr.count > self.maxQueueLength {
            _ = self.tasksArr.remove(at: 0)
            _ = self.tasksIndexPathArr.remove(at: 0)
        }
    }
    ///添加runloop的监听
    fileprivate func addRunloopObserver() {
        //获取当前RunLoop
        let runLoop: CFRunLoop = CFRunLoopGetCurrent()
        //定义一个上下文
        var context: CFRunLoopObserverContext = CFRunLoopObserverContext(version: 0, info: unsafeBitCast(self, to: UnsafeMutableRawPointer.self), retain: nil, release: nil, copyDescription: nil)
        //定义一个观察者
        if   let observer = CFRunLoopObserverCreate(kCFAllocatorDefault, CFRunLoopActivity.beforeWaiting.rawValue, true, 0, self.observerCallbackFunc(), &context){
            //添加当前RunLoop的观察者
            CFRunLoopAddObserver(runLoop, observer, .commonModes);
        }
    }
    ///回调函数
    func observerCallbackFunc() -> CFRunLoopObserverCallBack {
        return {(observer, activity, context) -> Void in
            if context == nil {//如果没有取到  直接返回
                return
            }
            // 崩溃的与原因不在这里  isFromADGuide 的问题 不应该取反
            //取出上下文 就是当前的vc
            let vc = unsafeBitCast(context, to: JCWFinancialFroductListVC.self)
            //取出任务
            if let unit = vc.tasksArr.first, let indexP = vc.tasksIndexPathArr.first {
                var result = false
                while (result == false && !vc.tasksArr.isEmpty && !vc.tasksIndexPathArr.isEmpty){
                    //执行任务
                    result = unit(indexP);
                    //干掉第一个任务
                    _ = vc.tasksArr.remove(at: 0)
                    _ = vc.tasksIndexPathArr.remove(at: 0)
                }
            }
        }
    }
}




//MARK:列表数据请求
extension JCWFinancialFroductListVC {
     fileprivate func loadData() {
        HYCNetService.productsList(withDict: ["genreId":self.type,"pageNo":self.pageManager.pageNo,"pageSize":JCWLimitNumberPageSize], onSuccess: { (result) in

            //隐藏加载框
            self.view.hideHUDLoading(0)

            //坑 一定记得加可选值 否则 footer可能不存在
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            
            if let result = result as? [String: Any], let title = result["title"] as? String {
                self.navigationItem.title = title
            }
            let resultList: [Any]? = JSON(result as Any).dictionaryValue["productsList"]?.arrayObject
            
            if self.pageManager.pageNo == JCWLimitNumberStarPage {
                self.pageManager.dataArr.removeAll()
                //下拉清除任务
                self.tasksArr.removeAll()
                self.tasksIndexPathArr.removeAll()
            }
            
            if let count = resultList?.count, count >= JCWLimitNumberPageSize{
                self.pageManager.pageNo += JCWLimitNumberStarPage
                weak var weakSelf = self
                self.tableView.mj_footer = JCWRefreshFooter(refreshingBlock: {
                    weakSelf?.loadData()
                })
            }else{
                self.tableView.mj_footer?.endRefreshingWithNoMoreData()
            }
            self.pageManager.dataArr.append(contentsOf: ProjectModel .mj_objectArray(withKeyValuesArray: resultList))
            self.tableView.reloadData()

            
        }) { (code, errMsg) in
            //隐藏加载框
            self.view.hideHUDLoading(0)
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
        }
    }
}






